var web = require("../../utils/loginWeb.js");

// 身份证 正则表达式
var idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
var phoneReg = /^1[3-9]\d{9}$/;

Page({
  data: {
    nameInputValue: "",
    picker: ["居民身份证", "港澳通行证", "台湾居民来往大陆通行证", "护照"],
    pickerIndex: 0,
    certifyCardType: "0",
    idcardInputValue: "",
    phoneInputValue: "",
    codeInputValue: "",
    noteYesText: "获取验证码",
    // 获取验证码按钮是否置灰
    isCanGetCode: true,
    // 登录按钮是否置灰
    isLoginCanClicked: true
  },
  onLoad() {
    // 读取缓存数据
    var pickerIndex = my.getStorageSync({ key: 'pickerIndex' }).data;
    var certifyCardType = my.getStorageSync({ key: 'certifyCardType' }).data;
    // 防止第一次登录为空
    if (pickerIndex == null) {
      pickerIndex = 0;
      certifyCardType = "0";
    }
    var time = Date.parse(new Date());
    var loginedTime = my.getStorageSync({ key: 'loginedTime' }).data;
    var codeInputValue = my.getStorageSync({ key: 'codeInputValue' }).data;
    // 如果上次登录时间超过12小时，则验证码自动置空
    if (time - loginedTime > 43200000) {
      codeInputValue = "";
      my.setStorageSync({ key: 'loginedTime', data: 0 });
    }
    this.setData({
      nameInputValue: my.getStorageSync({ key: 'nameInputValue' }).data,
      pickerIndex: pickerIndex,
      certifyCardType: certifyCardType,
      idcardInputValue: my.getStorageSync({ key: 'idcardInputValue' }).data,
      phoneInputValue: my.getStorageSync({ key: 'phoneInputValue' }).data,
      codeInputValue: codeInputValue
    });
  },
  picker(e) {
    var type = "0";
    if (e.detail.value == 0) {
      type = "0";
    } else if (e.detail.value == 1) {
      type = "B";
    } else if (e.detail.value == 2) {
      type = "C";
    } else {
      type = "Z";
    }
    this.setData({
      pickerIndex: e.detail.value
    });
    this.data.certifyCardType = type;
    // 写入缓存并把验证码清空
    my.setStorageSync({ key: 'pickerIndex', data: e.detail.value });
    my.setStorageSync({ key: 'certifyCardType', data: type });
    this.clearCode();
  },
  nameInput(e) {
    this.data.nameInputValue = e.detail.value.replace(/\s+/g, '');
    this.clearCode();
  },
  idcardInput(e) {
    this.data.idcardInputValue = e.detail.value.replace(/\s+/g, '');
    this.clearCode();
  },
  phoneInput(e) {
    this.data.phoneInputValue = e.detail.value.replace(/\s+/g, '');
    this.clearCode();
  },
  codeInput(e) {
    this.data.codeInputValue = e.detail.value.replace(/\s+/g, '');
    // 上次登录已变动
    my.setStorageSync({ key: 'loginedTime', data: 0 });
  },
  getCode() {
    // 如果不能点击，则直接返回
    if (!this.data.isCanGetCode) {
      return;
    }
    if (!this.checkInput()) {
      return;
    }

    // 开始倒计时
    web.client.loginCountDown(this);

    this.setData({
      isCanGetCode: false
    });
    this.clearCode();

    web.client.getCode(this.data.nameInputValue, this.data.certifyCardType,
      this.data.idcardInputValue, this.data.phoneInputValue, this);

    this.fourStorage();
  },
  login() {
    // 如果不能点击，则直接返回
    if (!this.data.isLoginCanClicked) {
      return;
    }
    if (!this.checkInput()) {
      return;
    }
    if (this.data.codeInputValue == null || this.data.codeInputValue == "" || this.data.codeInputValue.length < 6) {
      my.showToast({ content: "请输入正确的验证码", duration: 2000 });
      return;
    }

    if (my.getStorageSync({ key: 'loginedTime' }).data != 0) {
      my.navigateTo({ url: "../buildingList/buildingList" });
    } else {
      this.data.isLoginCanClicked = false;
      web.client.login(this.data.phoneInputValue, this.data.codeInputValue,
        this.data.nameInputValue, this.data.certifyCardType, this.data.idcardInputValue, this);
      this.fourStorage();
      my.setStorageSync({ key: 'codeInputValue', data: this.data.codeInputValue });
    }
  },
  // 清空验证码输入框
  clearCode() {
    if (this.data.codeInputValue != null && this.data.codeInputValue != "") {
      this.setData({ codeInputValue: "" });
      // 写入缓存并把验证码清空
      my.setStorageSync({ key: 'codeInputValue', data: "" });
    }
  },
  // 检查输入框
  checkInput() {
    if (this.data.nameInputValue == null || this.data.nameInputValue == "") {
      my.showToast({ content: "姓名不能为空", duration: 2000 });
      return false;
    }
    if (this.data.idcardInputValue == null || this.data.idcardInputValue == "") {
      my.showToast({ content: "证件号码不能为空", duration: 2000 });
      return false;
    }
    if (this.data.certifyCardType == "0" && !idcardReg.test(this.data.idcardInputValue)) {
      my.showToast({ content: "请输入您真实的身份证号码", duration: 2000 });
      return false;
    }
    if (this.data.phoneInputValue == null || this.data.phoneInputValue == "" || !phoneReg.test(this.data.phoneInputValue)) {
      my.showToast({ content: "请输入正确的手机号", duration: 2000 });
      return false;
    }
    return true;
  },
  fourStorage() {
    my.setStorageSync({ key: 'nameInputValue', data: this.data.nameInputValue });
    my.setStorageSync({ key: 'certifyCardType', data: this.data.certifyCardType });
    my.setStorageSync({ key: 'idcardInputValue', data: this.data.idcardInputValue });
    my.setStorageSync({ key: 'phoneInputValue', data: this.data.phoneInputValue });
  }
});
